<?php

function convertDoc2HTML($txt){
        $len = strlen($txt);
        $res = "";
        for($i = 0; $i < $len; ++$i) {
            $ord = ord($txt{$i});
            // check only non-standard chars         
            if($ord >= 126){
                $res .= "&#".$ord.";";
            }
            else {
                // escape ", ' and \ chars
                switch($ord){
                    case 34 :
                        $res .= "\\\"";
                        break;
                    case 39 :
                        $res .= "\'";
                        break;
                    case 92 :
                        $res .= "\\\\";
                        break;                   
                    default : // the rest does not have to be modified
                        $res .= $txt{$i};
                }                   
            }
        }
        return $res;
}

function htmlentities2unicodeentities ($input) {
  $htmlEntities = array_values (get_html_translation_table (HTML_ENTITIES, ENT_QUOTES));
  $entitiesDecoded = array_keys   (get_html_translation_table (HTML_ENTITIES, ENT_QUOTES));
  $num = count ($entitiesDecoded);
  for ($u = 0; $u < $num; $u++) {
    $utf8Entities[$u] = '&#'.ord($entitiesDecoded[$u]).';';
  }
  return str_replace ($htmlEntities, $utf8Entities, $input);
}


function isValidUser($username,$password) {

//$password = md5($password);

$squery = "select * from Users where Username='".$username."' and Password='".$password."'";

$result = mysql_query($squery);



if($userdata = mysql_fetch_assoc($result))

{

	return 1;

}

else

	return 0;

}

function get_user_id($username,$password) {

//$password = md5($password);

$squery = "select ID from Users where Username='".$username."' and Password='".$password."'";
$result = mysql_query($squery);
if(mysql_num_rows($result))
{
$row = mysql_fetch_assoc($result);
$str = $row['ID'];
}
else
{
$str = 0;
}

return $str;

}





function getXML($resultID) {



$xml_output.= "<entries>\n"; 

for($x = 0 ; $x < mysql_num_rows($resultID) ; $x++){ 

   

	$xml_output.= "\t<entry>\n"; 

	$row = mysql_fetch_assoc($resultID);

	 $numfields = mysql_num_fields($resultID);

	for($y=0;$y<$numfields;$y++)

	{

		$fieldname =   mysql_field_name($resultID,$y);

		/*

		$row[$fieldname] = str_replace("&", "&amp;", $row[$fieldname]); 

        $row[$fieldname] = str_replace("<", "&lt;", $row[$fieldname]); 

        $row[$fieldname] = str_replace(">", "&gt;", $row[$fieldname]); 

        $row[$fieldname] = str_replace("\"", "&quot;", $row[$fieldname]); */

//$tmpfielddata = convertDoc2HTML($row[$fieldname]);
//$newfielddata = htmlentities2unicodeentities($tmpfielddata);

		 //$xml_output.= "\t\t<$fieldname><![CDATA[" . $newfielddata . "]]></$fieldname>\n"; 
		 $xml_output.= "\t\t<$fieldname><![CDATA[" . addslashes($row[$fieldname]) . "]]></$fieldname>\n"; 

	}	

    

    

    $xml_output.= "\t</entry>\n";

	

	

} 

$xml_output .= "</entries>";  

return $xml_output;

}

function getXML_agent($resultID) {



$xml_output.= "<entries>\n"; 

for($x = 0 ; $x < mysql_num_rows($resultID) ; $x++){ 

   

	$xml_output.= "\t<entry>\n"; 

	$row = mysql_fetch_assoc($resultID);

	 $numfields = mysql_num_fields($resultID);

	for($y=0;$y<$numfields;$y++)

	{

		$fieldname =   mysql_field_name($resultID,$y);

		/*

		$row[$fieldname] = str_replace("&", "&amp;", $row[$fieldname]); 

        $row[$fieldname] = str_replace("<", "&lt;", $row[$fieldname]); 

        $row[$fieldname] = str_replace(">", "&gt;", $row[$fieldname]); 

        $row[$fieldname] = str_replace("\"", "&quot;", $row[$fieldname]); */

$tmpfielddata = convertDoc2HTML($row[$fieldname]);
$newfielddata = htmlentities2unicodeentities($tmpfielddata);

		 $xml_output.= "\t\t<$fieldname><![CDATA[" . ($newfielddata) . "]]></$fieldname>\n"; 
		 //$xml_output.= "\t\t<$fieldname><![CDATA[" . addslashes($row[$fieldname]) . "]]></$fieldname>\n"; 

	}	

    

    

    $xml_output.= "\t</entry>\n";

	

	

} 

$xml_output .= "</entries>";  

return $xml_output;

}

function getXML_group($resultID) {


//$xml_output.= "<entries>\n"; 

for($x = 0 ; $x < mysql_num_rows($resultID) ; $x++){ 

   

	$xml_output.= "\t<entry>\n"; 

	$row = mysql_fetch_assoc($resultID);

	 $numfields = mysql_num_fields($resultID);

	for($y=0;$y<$numfields;$y++)

	{

		$fieldname =   mysql_field_name($resultID,$y);

		 $xml_output.= "\t\t<$fieldname><![CDATA[" . addslashes($row[$fieldname]) . "]]></$fieldname>\n"; 

	}	


    $xml_output.= "\t</entry>\n";


} 

//$xml_output .= "</entries>";  

return $xml_output;

}

function getXML_start($resultID)
{




$xml_output.= "<entries>\n"; 

for($x = 0 ; $x < mysql_num_rows($resultID) ; $x++){ 

   

	$xml_output.= "\t<entry>\n"; 

	$row = mysql_fetch_assoc($resultID);

	 $numfields = mysql_num_fields($resultID);

	for($y=0;$y<$numfields;$y++)

	{

		$fieldname =   mysql_field_name($resultID,$y);

		/*

		$row[$fieldname] = str_replace("&", "&amp;", $row[$fieldname]); 

        $row[$fieldname] = str_replace("<", "&lt;", $row[$fieldname]); 

        $row[$fieldname] = str_replace(">", "&gt;", $row[$fieldname]); 

        $row[$fieldname] = str_replace("\"", "&quot;", $row[$fieldname]); */

		 $xml_output.= "\t\t<$fieldname><![CDATA[" . $row[$fieldname] . "]]></$fieldname>\n"; 

	}	

    

    

    $xml_output.= "\t</entry>\n";

	

	

} 

//$xml_output .= "</entries>";  

return $xml_output;


}

function getXML_end($resultID)
{




//$xml_output.= "<entries>\n"; 

for($x = 0 ; $x < mysql_num_rows($resultID) ; $x++){ 

   

	$xml_output.= "\t<entry>\n"; 

	$row = mysql_fetch_assoc($resultID);

	 $numfields = mysql_num_fields($resultID);

	for($y=0;$y<$numfields;$y++)

	{

		$fieldname =   mysql_field_name($resultID,$y);

		/*

		$row[$fieldname] = str_replace("&", "&amp;", $row[$fieldname]); 

        $row[$fieldname] = str_replace("<", "&lt;", $row[$fieldname]); 

        $row[$fieldname] = str_replace(">", "&gt;", $row[$fieldname]); 

        $row[$fieldname] = str_replace("\"", "&quot;", $row[$fieldname]); */

		 $xml_output.= "\t\t<$fieldname><![CDATA[" . $row[$fieldname] . "]]></$fieldname>\n"; 

	}	

    

    

    $xml_output.= "\t</entry>\n";

	

	

} 

$xml_output .= "</entries>";  

return $xml_output;


}

function getXML2($resultID)
{




$xml_output.= "<entries>\n"; 

for($x = 0 ; $x < mysql_num_rows($resultID) ; $x++){ 

   

	$xml_output.= "\t<entry>\n"; 

	$row = mysql_fetch_assoc($resultID);

	 $numfields = mysql_num_fields($resultID);

	for($y=0;$y<$numfields;$y++)

	{

		$fieldname =   mysql_field_name($resultID,$y);

		/*

		$row[$fieldname] = str_replace("&", "&amp;", $row[$fieldname]); 

        $row[$fieldname] = str_replace("<", "&lt;", $row[$fieldname]); 

        $row[$fieldname] = str_replace(">", "&gt;", $row[$fieldname]); 

        $row[$fieldname] = str_replace("\"", "&quot;", $row[$fieldname]); */

		 $xml_output.= "\t\t<$fieldname><![CDATA[" . urlencode($row[$fieldname]) . "]]></$fieldname>\n"; 

	}	

    

    

    $xml_output.= "\t</entry>\n";

	

	

} 

$xml_output .= "</entries>";  

return $xml_output;


}


function get_id($username)
{
$query = "select ID from Users where Username='".$username."'";
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);
return $row['ID'];
}





?>